package com.ldk.base.framework.util.sms;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.BizResult;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * 阿里大于
 * @author xiangwl
 * 2017年4月12日
 * com.ldk.base.framework.util.sms
 * AliDayu
 */
public class AliDayu implements ISmsApi {
	
	final Logger logger = LoggerFactory.getLogger(AliDayu.class);
	final String serverUrl = "http://gw.api.taobao.com/router/rest";
	/**
	 * 短信验证码模版
	 */
	public static final String TEMPLETE_VALICODE = "SMS_61340123";

	/**
	 * 阿里大于有几个特殊地方
	 * 1、content为短信模块对应参数的json值，如：{"code":"212121"}
	 * 2、短信签名不能为空
	 * 3、短信模版不能为空
	 */
	@Override
	public Boolean send(SmsConfig config) {
		if(config == null) return false;
		//appKey=23749306
		//appSecret=eed225a68792fb6e0fe57e83712f9712
		TaobaoClient taobaoClient = new DefaultTaobaoClient(serverUrl, config.getAccessKey(), config.getAccessPwd());
		AlibabaAliqinFcSmsNumSendRequest request = new AlibabaAliqinFcSmsNumSendRequest();
		request.setExtend("123456");
		request.setSmsType("normal");
		request.setSmsFreeSignName(config.getSignName());
		request.setRecNum(config.getPhonNos());
		request.setSmsParamString(config.getContent());
		request.setSmsTemplateCode(config.getTempleteCode());
		try {
			AlibabaAliqinFcSmsNumSendResponse response = taobaoClient.execute(request);
			BizResult rst= response != null ? response.getResult() : null;
			return rst != null && rst.getSuccess();
			
		} catch (ApiException e) {
			e.printStackTrace();
			logger.error("AliDayu.ApiException="+e.getMessage()+",code="+e.getErrCode()+",msg="+e.getErrMsg());
		}
		
		return false;
	}

}
